package io.renren.modules.zhongyi.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.zhongyi.utils.HttpRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/zy_wechat")
@Api(tags = "微信小程序")
public class ZyWeChatController {


    @PostMapping("getOpenId")
    @ApiOperation("获取微信openid以及用户其他信息")
    public R getOpenIdOrSession(@RequestBody Map map) {
        String code = (String) map.get("code");
        System.out.println("code----------" + map.get("code"));
        if (code == null || code.length() <= 0) {
            return R.ok().put("data", "code 不存在");
        }
        Map<String, Object> map2 = new HashMap<>();
        map2.put("appid", "wx118ee23fb49edc57");
        map2.put("secret", "c257ef0adf183972e823830221a9848d");
        map2.put("js_code", code);
        map2.put("grant_type", "authorization_code");
        JSONObject jsonObject = null;
        try {
            String response = HttpRequestUtil.httpGet("https://api.weixin.qq.com/sns/jscode2session", map2);
            jsonObject = JSON.parseObject(response);

            System.out.println(jsonObject);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok().put("data", jsonObject.getString("openid"));
    }


}
